package cn.cqnu.controller.front;

import cn.cqnu.domin.dto.RegisterDto;
import cn.cqnu.domin.entity.Student;
import cn.cqnu.domin.vo.ResponseResult;
import cn.cqnu.global.SystemReturnEnum;
import cn.cqnu.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @作者：张世豪
 * @version：1.0
 */
@RestController
public class StudentRegisterController {

    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private StudentService studentService;

    @PostMapping("/register")
    private ResponseResult register(@RequestBody RegisterDto dto) {
        System.out.println(dto);

        String s = redisTemplate.opsForValue().get(dto.getKey());
        if (s == null || !s.equals(dto.getCaptcha()))
            return ResponseResult.error(SystemReturnEnum.CAPTCHA_ERR);

        Student temp = new Student();
        temp.setStudentId(dto.getId());
        temp.setStudentName(dto.getName());
        temp.setStudentSchool(dto.getSchool());
        temp.setStudentSubject(dto.getSubject());
        temp.setStudentPassword(dto.getPassword());
        return studentService.register(temp);
    }
}
